package
{
	import com.indiemaps.mapping.data.parsers.e00.E00Parser;
	import com.indiemaps.mapping.data.parsers.e00.E00Tools;
	import com.indiemaps.mapping.projections.LambertConformalConic;
	
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	import flash.net.URLLoader;
	import flash.net.URLLoaderDataFormat;
	import flash.net.URLRequest;
	import flash.utils.Dictionary;
	
	/**
	 * A simple application of the E00Parser and E00Tools classes.
	 * Simply loads, projects, and draws an e00 file of the United States at run-time.
	 * Note: loading/projecting/drawing may take up to a couple dozen seconds (though should only take a few).  
	 * No progress bar or status indicators are included in the example. */
	[SWF(backgroundColor='#ffffff', width='1000', height='500')]
	public class E00BasicDrawingExample extends Sprite
	{
		
		protected static const BOUNDS : Rectangle = new Rectangle( 0, 0, 1000, 500 );
		
		protected static var STANDARD_PARALLEL_1 : Number = 33.7;
		protected static var STANDARD_PARALLEL_2 : Number = 35.8;
		protected static var LATITUDE_ORIGIN : Number = 34.7;
		protected static var LONGITUDE_ORIGIN : Number = -92.4;
		
		private var e00Location : String = 'data/e00/st99_d00.e00';
		
		private var mapContainer : Sprite = new Sprite();
		
		public function E00BasicDrawingExample()
		{
			addChild( mapContainer );
			
			// load e00 data
			var dataLoader:URLLoader = new URLLoader();
			dataLoader.dataFormat = URLLoaderDataFormat.TEXT;
			dataLoader.load( new URLRequest( e00Location ) );
			dataLoader.addEventListener( Event.COMPLETE, onE00LoadingComplete );
		}
		
		private function onE00LoadingComplete( e : Event ) : void
		{
			var e00Text : String = e.target.data as String;
			
			// parse the e00 ascii file text
			var data : Object = E00Parser.parse( e00Text );
			
			// get a dictionary of the drawn features, projected in lambert's conformal conic (features will be drawn a default grey with white outlines due to lack of a passed-in graphics callback function)
			var featureSprites : Dictionary = 	E00Tools.drawAllFeatures( data, data[ 'ifo' ][ 'PAT' ][ 'data' ], 'STATE', projectionFunction, BOUNDS.width, BOUNDS.height, null, [ '02', '', 72, 15 ] );
			
			// features are already scaled and positioned correctly (based on bounds specified above) and need only be added to the display list
			for each ( var featureSprite : Object in featureSprites ) {
				// the returned featureSprites Dictionary contains only Sprites save for 3 numeric properties: 'zoom', 'xAdjustment', and 'yAdjustment'
				if ( featureSprite is Sprite )
					mapContainer.addChild( featureSprite as Sprite );
			}
		}
		
		// uses my LambertConformalConic projection class, a projection often used to display the continental U.S.A.
		private function projectionFunction( point : Object ) : Object
		{	
			return LambertConformalConic.projectPoint( point.y, point.x, 1000, -1000, 500, -500, STANDARD_PARALLEL_1, STANDARD_PARALLEL_2, LATITUDE_ORIGIN, LONGITUDE_ORIGIN );
		}
	}
}